﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Random=UnityEngine.Random;

public class BoardManager : MonoBehaviour
{
    [Serializable]
    public class Count
    {
        public int minimun;
        public int maximun;

        public Count(int min, int max)
        {
            this.minimun = min;
            this.maximun = max;
        }
    }

    public int columns = 8;
    public int rows = 8;
    public Count wallCount=new Count(5,9);
    public Count foodCount=new Count(1,5);
    public GameObject exit;
    public GameObject[] floorTiles;
    public GameObject[] wallTiles;
    public GameObject[] foodTiles;
    public GameObject[] enemyTiles;
    public GameObject[] outerWallTiles;

    private Transform boardHolder;
    private List<Vector3> gridPositions = new List<Vector3>();

    void InitialiseList()
    {
        this.gridPositions.Clear();
        for (int x = 1; x < this.columns-1; x++)
        {
            for (int y = 1; y < this.rows-1; y++)
            {
                this.gridPositions.Add(new Vector3(x,y,0f));
            }
        }
    }

    void BoardSetup()
    {
        this.boardHolder=new GameObject("Board").transform;
        for (int x = -1; x < columns+1; x++)
        {
            for (int y = -1; y < rows+1; y++)
            {
                GameObject toInstantiate = floorTiles[Random.Range(0, this.floorTiles.Length)];
                if (x == -1 || x == this.columns || y == -1 || y == this.rows)
                {
                    toInstantiate = this.outerWallTiles[Random.Range(0, this.outerWallTiles.Length)];
                }

                GameObject instance = Instantiate(toInstantiate,new Vector3(x,y,0f),Quaternion.identity) as GameObject;
                
                instance.transform.SetParent(boardHolder);

            }
        }
    }

    Vector3 RandomPosition()
    {
        int randomIndex = Random.Range(0, this.gridPositions.Count);
        Vector3 randomPosition = this.gridPositions[randomIndex];
        this.gridPositions.RemoveAt(randomIndex);
        return randomPosition;
    }

    void LayoutObjectAtRandom(GameObject[] titleArray, int minimum, int maximun)
    {
        int objectCount = Random.Range(minimum, maximun + 1);
        for (int i = 0; i < objectCount; i++)
        {
            Vector3 randomPosition = RandomPosition();
            GameObject titleChoice = titleArray[Random.Range(0, titleArray.Length)];
            Instantiate(titleChoice, randomPosition, Quaternion.identity);
        }
    }

    public void SetupScene(int level)
    {
        BoardSetup();
        InitialiseList();
        LayoutObjectAtRandom(wallTiles,wallCount.minimun,wallCount.maximun);
        LayoutObjectAtRandom(foodTiles,foodCount.minimun,foodCount.maximun);
        int enemyCount = (int) Mathf.Log(level, 2f);
        LayoutObjectAtRandom(enemyTiles,enemyCount,enemyCount);
        Instantiate(exit, new Vector3(columns - 1, rows - 1, 0f), Quaternion.identity);


    }
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
